
import { observable, action, computed } from 'mobx'
import IStore from './IStore';
import { StoragerManager } from '../managers/StoragerManager';

export class UserStore implements IStore {
    public prefix: string = 'userInfo';

    private static _ins: UserStore;

    public static get Ins() {
        return this._ins = this._ins ? this._ins : new UserStore();
    }

    // 音乐大小
    @observable
    public musicVoice: number;


    // 音效大小
    @observable
    public effectVoice: number;


    // 金币
    @observable
    public money: number;

    /**
     * 当前关卡
     */
    @observable
    public level: number;


    /**
     * 当前引导进度
     */
    @observable
    public guideId: number;

    @computed
    public get is30() { return this.money == 30 }


    // 初始化用户信息
    @action
    public init() {
        StoragerManager.load(() => {
            console.log('loadStorage success!');
        }, () => {
            console.log('loadStorage fail!');
            this.money = 9;
            this.level = 0;
            this.musicVoice = .5;
            this.effectVoice = .5;
            this.guideId = 0;
        });

    }

    @action
    public clear() {
        StoragerManager.clear();
    }

    // 设置音乐大小
    @action
    public setMusic(val: number) {
        this.musicVoice = val;
        StoragerManager.save();
    }

    // 设置音效大小
    @action
    public setEffect(val: number) {
        this.effectVoice = val;
        StoragerManager.save();
    }

    // 增加1分
    @action
    public mute() {
        this.musicVoice = this.effectVoice = 0;
        StoragerManager.save();
    }

    // 增加1分
    @action
    public addmoney() {
        this.money++;
        StoragerManager.save();
    }
    // 设置音效大小
    @action
    public setGuide(id: number) {
        this.guideId = id;
        StoragerManager.save();
    }

    /**
     * 通关关卡
     */
    @action
    public addLevel() {
        this.level++;
        StoragerManager.save();
    }

    public get save(): saveData {
        let obj: saveData = {
            money: UserStore.Ins.money,
            level: UserStore.Ins.level,
            musicVoice: UserStore.Ins.musicVoice,
            effectVoice: UserStore.Ins.effectVoice,
            guideId: UserStore.Ins.guideId,
        }
        return obj;
    }
    public parse(obj: saveData) {
        UserStore.Ins.money = obj.money;
        UserStore.Ins.level = obj.level;
        UserStore.Ins.musicVoice = obj.musicVoice;
        UserStore.Ins.effectVoice = obj.effectVoice;
        UserStore.Ins.guideId = obj.guideId;
    }
}
export interface saveData {
    money?: number;
    level?: number;
    musicVoice?: number;
    effectVoice?: number;
    guideId?: number;
}